package com.wxblockchain.prometheum.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wxblockchain.core.utils.LogUtil;
import com.wxblockchain.prometheum.managers.FlutterFragmentManager;

import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.android.RenderMode;
import io.flutter.embedding.android.TransparencyMode;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.view.FlutterMain;

public class BaseFlutterFragment extends FlutterFragment {
    private String TAG  = "BaseFlutterFragment";
//
//    @Override
//    public SplashScreen provideSplashScreen() {
//        Drawable splash = getActivity().getResources().getDrawable(R.drawable.ic_launcher_background);
//
//        return new DrawableSplashScreen(splash);
//    }


    @Override
    public void onAttach(Context context) {
        LogUtil.e(TAG,"onAttach");

        super.onAttach(context);

        FlutterFragmentManager.getInstance().register(getCacheRegisterKey(),this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FlutterView flutterView = new FlutterView(getActivity(), FlutterView.RenderMode.texture, FlutterView.TransparencyMode.transparent);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        flutterView.setLayoutParams(lp);

        flutterView.attachToFlutterEngine(FlutterEngineCache.getInstance().get(getCachedEngineId()));

        return flutterView;
    }

    @Override
    public void onResume() {
        super.onResume();

        LogUtil.e(TAG,"onResume");
    }

    @Override
    public void onPostResume() {
        super.onPostResume();

        LogUtil.e(TAG,"onPostResume");
    }

    @Override
    public void onUserLeaveHint() {
        super.onUserLeaveHint();

        LogUtil.e(TAG,"onUserLeaveHint");
    }

    @Override
    public void onStart() {
        super.onStart();

        LogUtil.e(TAG,"onStart");
    }

    @Override
    public void onPause() {
        super.onPause();

        LogUtil.e(TAG,"onPause");
    }

    @Override
    public void onStop() {
        super.onStop();

        LogUtil.e(TAG,"onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        LogUtil.e(TAG,"onDestroyView");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        LogUtil.e(TAG,"onBackPressed");
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        LogUtil.e(TAG,"onNewIntent");
    }

    @Override
    public void onDetach() {
        super.onDetach();

        LogUtil.e(TAG,"onDetach");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LogUtil.e(TAG,"onActivityCreated");
    }

    @Override
    public void onFlutterUiDisplayed() {
        super.onFlutterUiDisplayed();

        LogUtil.e(TAG,"onFlutterUiDisplayed");
    }

    @Override
    public void onFlutterUiNoLongerDisplayed() {
        super.onFlutterUiNoLongerDisplayed();

        LogUtil.e(TAG,"onFlutterUiNoLongerDisplayed");
    }

    protected String getCacheRegisterKey() {
        return "";
    }

    @Override
    public FlutterShellArgs getFlutterShellArgs() {
        return new FlutterShellArgs(new String[] {}
        );
    }

    @Override
    public String getDartEntrypointFunctionName() {
        return "main";
    }

    @Override
    public String getCachedEngineId() {
        return null;
    }

    @Override
    public boolean shouldAttachEngineToActivity() {
        return false;
    }

    @Override
    public String getAppBundlePath() {
        return FlutterMain.findAppBundlePath();
    }

    @NonNull
    @Override
    public RenderMode getRenderMode() {
        return RenderMode.texture;
    }

    @NonNull
    @Override
    public TransparencyMode getTransparencyMode() {
        return TransparencyMode.transparent;
    }

    @Override
    public boolean shouldDestroyEngineWithHost() {
        return false;
    }
}
